<?xml version="1.0" encoding="utf-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
 <title type="text">Gnaily's Blog: Posts tagged 'Spring'</title>
 <link rel="self" href="http://yangliang.site/feeds\Spring.atom.xml" />
 <link href="http://yangliang.site/tags\Spring.html" />
 <id>urn:http-yangliang-site:-tags-Spring-html</id>
 <updated>2018-08-31T16:00:00Z</updated>
 <entry>
  <title type="text">Spring Framework</title>
  <link rel="alternate" href="http://yangliang.site/2018\09\spring-framework.html?utm_source=Spring&amp;utm_medium=Atom" />
  <id>urn:http-yangliang-site:-2018-09-spring-framework-html</id>
  <published>2018-08-31T16:00:00Z</published>
  <updated>2018-08-31T16:00:00Z</updated>
  <author>
   <name>Gnaily</name></author>
  <content type="html">
&lt;p&gt;[TOC]&lt;/p&gt;

&lt;h1 id="spring"&gt;Spring&lt;/h1&gt;

&lt;blockquote&gt;
 &lt;p&gt;Spring 参考文档&lt;/p&gt;
 &lt;p&gt;Spring core&lt;/p&gt;
 &lt;p&gt;https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html&lt;/p&gt;
 &lt;p&gt;Spring mvc&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Sping将程序看成由各个组件相互组装而成，&lt;code&gt;抽象出组件和组装组件的容器&lt;/code&gt;。应用程序中通过组件的配置信息（xml、java注解、java代码）告诉容器如何初始化、配置和组件组件，Spring扫描配置信息，然后按照配置信息组装出程序。这就是Spring的核心原理。&lt;/p&gt;

&lt;div class="figure"&gt;&lt;img src="C:UsersGnailyAppDataLocalTemp1523797001169.png" alt="1523797001169" /&gt;
 &lt;p class="caption"&gt;1523797001169&lt;/p&gt;&lt;/div&gt;

&lt;h3 id="应用程序组件的表示spring-bean"&gt;应用程序组件的表示——Spring Bean&lt;/h3&gt;

&lt;p&gt;Spring使用Bean来表示应用程序组件，在Spring中每一个应用程序组件都是一个Bean实体。Spring的Bean可以是任何形式的POJO。&lt;/p&gt;

&lt;p&gt;Bean的作用域：&lt;/p&gt;

&lt;p&gt;| Scope | Description | | &amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash; | &amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash; | | &lt;a href="https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html#beans-factory-scopes-singleton"&gt;singleton&lt;/a&gt; | (Default) Scopes a single bean definition to a single object instance per Spring IoC container. | | &lt;a href="https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html#beans-factory-scopes-prototype"&gt;prototype&lt;/a&gt; | Scopes a single bean definition to any number of object instances. | | &lt;a href="https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html#beans-factory-scopes-request"&gt;request&lt;/a&gt; | Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring &lt;code&gt;ApplicationContext&lt;/code&gt;. | | &lt;a href="https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html#beans-factory-scopes-session"&gt;session&lt;/a&gt; | Scopes a single bean definition to the lifecycle of an HTTP &lt;code&gt;Session&lt;/code&gt;. Only valid in the context of a web-aware Spring &lt;code&gt;ApplicationContext&lt;/code&gt;. | | &lt;a href="https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html#beans-factory-scopes-application"&gt;application&lt;/a&gt; | Scopes a single bean definition to the lifecycle of a &lt;code&gt;ServletContext&lt;/code&gt;. Only valid in the context of a web-aware Spring &lt;code&gt;ApplicationContext&lt;/code&gt;. | | &lt;a href="https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/web.html#websocket-stomp-websocket-scope"&gt;websocket&lt;/a&gt; | Scopes a single bean definition to the lifecycle of a &lt;code&gt;WebSocket&lt;/code&gt;. Only valid in the context of a web-aware Spring &lt;code&gt;ApplicationContext&lt;/code&gt;. |&lt;/p&gt;

&lt;p&gt;Bean的配置：&lt;/p&gt;

&lt;p&gt;xml配置Bean：在xml文件中配置一个Bean时，用一个唯一的id来标识这个Bean,用class指定该Bean的类的全限定名（包名.类名）。&lt;/p&gt;

&lt;div class="brush: xml"&gt;
 &lt;pre&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"&amp;gt;
 
    &amp;lt;bean id="..." class="..."&amp;gt;
        &amp;lt;!-- collaborators and configuration for this bean go here --&amp;gt;
    &amp;lt;/bean&amp;gt;
 
    &amp;lt;bean id="..." class="..."&amp;gt;
        &amp;lt;!-- collaborators and configuration for this bean go here --&amp;gt;
 
    &amp;lt;!-- more bean definitions go here --&amp;gt;
 
&amp;lt;/beans&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;注解配置Bean：在POJO的类上添加@Bean注解。&lt;/p&gt;

&lt;h3 id="应用程序组件的组装spring-依赖注入与控制反转"&gt;应用程序组件的组装——Spring 依赖注入与控制反转&lt;/h3&gt;

&lt;h4 id="依赖注入dependency-injection"&gt;依赖注入（Dependency Injection）&lt;/h4&gt;

&lt;p&gt;一个对象不在自己的内部构造出它所依赖的对象，它所依赖的对象由第三方构造然后通过该对象的&lt;code&gt;构造方法&lt;/code&gt;或者&lt;code&gt;setter方法&lt;/code&gt;注入进来。&lt;/p&gt;

&lt;h4 id="控制反转容器ioc-container"&gt;控制反转容器（IoC Container）&lt;/h4&gt;

&lt;p&gt;Spring通过一个容器来根据一个对象的配置信息（xml、java注解、java代码）组装所依赖的Bean实例，并将组装出的Bean实例注入到该对象中。Spring中的这个容器控制着所有应用组件（Bean对象）的整个生命周期，从如何产生到如何销毁，而原本这些对象的生命周期都是由其依赖的对象直接控制的，这个容器被叫做控制反转容器，因为它反转了对象的控制权。Spring容器是Spring的核心。&lt;/p&gt;

&lt;ul&gt;
 &lt;li&gt;
  &lt;p&gt;&lt;code&gt;org.springframework.beans&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
 &lt;li&gt;
  &lt;p&gt;&lt;code&gt;org.springframework.context&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h3 id="应用程序资源spring-resource"&gt;应用程序资源——Spring Resource&lt;/h3&gt;

&lt;p&gt;Spring内部自定义的资源：&lt;/p&gt;

&lt;ul&gt;
 &lt;li&gt;
  &lt;p&gt;UrlResource：wraps a &lt;code&gt;java.net.URL&lt;/code&gt;, and may be used to access any object that is normally accessible via a URL, such as files, an HTTP target, an FTP target, etc.&lt;/p&gt;&lt;/li&gt;
 &lt;li&gt;ClassPathResource：represents a resource which should be obtained from the classpath.This uses either the thread context class loader, a given class loader, or a given class for loading resources.&lt;/li&gt;
 &lt;li&gt;FileSystemResource：a &lt;code&gt;Resource&lt;/code&gt; implementation for &lt;code&gt;java.io.File&lt;/code&gt; handles. It obviously supports resolution as a &lt;code&gt;File&lt;/code&gt;, and as a &lt;code&gt;URL&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;ServletContextResource：a &lt;code&gt;Resource&lt;/code&gt; implementation for &lt;code&gt;ServletContext&lt;/code&gt; resources, interpreting relative paths within the relevant web application’s root directory&lt;/li&gt;
 &lt;li&gt;InputStreamResource：implementation for a given &lt;code&gt;InputStream&lt;/code&gt;. This should only be used if no specific &lt;code&gt;Resource&lt;/code&gt; implementation is applicable.&lt;/li&gt;
 &lt;li&gt;ByteArrayResource：a &lt;code&gt;Resource&lt;/code&gt; implementation for a given byte array&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;资源加载器（ResourceLoader）：&lt;/p&gt;

&lt;p&gt;资源加载器用来实现在应用程序中加载资源。 Spring中ResourceLoader接口定义如下所示。 Spring中所用的应用程序上下文都实现了该接口。所以如果在应用程序中想要获取资源，可以通过应用程序上下文来获取。例如applicationContext.getResource(&amp;ldquo;http://myhost.com/resource/path/myTemplate.txt&amp;rdquo;)；&lt;/p&gt;

&lt;div class="brush: java"&gt;
 &lt;pre&gt;&lt;code&gt;public interface ResourceLoader {
 
    Resource getResource(String location);
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="应用程序数据处理验证数据绑定和类型转换"&gt;应用程序数据处理——验证、数据绑定和类型转换&lt;/h3&gt;

&lt;h4 id="validation"&gt;Validation&lt;/h4&gt;

&lt;h4 id="data-binding"&gt;Data Binding&lt;/h4&gt;

&lt;h4 id="type-convertion"&gt;Type Convertion&lt;/h4&gt;

&lt;p&gt;Spring 提供了一个通用的类型转换系统,可以用来进行类型之间的转换。其接口定义如下：&lt;/p&gt;

&lt;div class="brush: java"&gt;
 &lt;pre&gt;&lt;code&gt;package org.springframework.core.convert.converter;
 
public interface Converter&amp;lt;S, T&amp;gt; {
 
    T convert(S source);
 
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="切面编程spring-aop"&gt;切面编程——Spring AOP&lt;/h3&gt;

&lt;h4 id="aop原理"&gt;AOP原理&lt;/h4&gt;

&lt;h4 id="spring-aop中的概念"&gt;Spring AOP中的概念&lt;/h4&gt;

&lt;p&gt;AOP框架是Spring的一个关键组件。AOP的应用如权限管理、日志记录、事务的。Spring中AOP的概念：&lt;/p&gt;

&lt;ul&gt;
 &lt;li&gt;
  &lt;p&gt;&lt;em&gt;Aspect&lt;/em&gt; ：切面，对跨越多个类的关注点的模块化抽象。&lt;/p&gt;&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;Join point&lt;/em&gt; ：接入点，表示程序执行期间的一个位置，比如程序中某个方法调用前的地方，方法调用异常的地方或者方法调用结束的地方，这些位置可以接入一些切面的行为。&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;Advice&lt;/em&gt; ：表示切面在特殊接入点（join point）的行为。&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;Pointcut&lt;/em&gt; ：切点，对接入点（join point）的匹配预测。Advice 被关联到一个pointcut表达式，它会在匹配 pointcut表达式的任何接入点处执行行。&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;Introduction&lt;/em&gt; ：在一个类型上声明附加的方法和字段&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;Target object&lt;/em&gt; : 目标对象。&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;AOP proxy&lt;/em&gt; ：AOP 框架为实现切面契约而创建的对象，它是Target object的代理对象。&lt;/li&gt;
 &lt;li&gt;&lt;em&gt;Weaving&lt;/em&gt; ：&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;JDk动态代理实现AOP：&lt;/p&gt;

&lt;div class="brush: java"&gt;
 &lt;pre&gt;&lt;code&gt;public abstract class Aspect implements InvocationHandler {
 
    private Object target;
 
    public Aspect(Object target) {
        this.target = target;
    }
 
    /**
     * Returns target object.
     */
    public final Object getTarget() {
        return this.target;
    }
 
    /**
     * Runs before targets method. Returns {@code true} if target method
     * should run.
     */
    public abstract boolean before(Object target, Method method, Object[] args);
 
    /**
     * Runs after targets method. Returns {@code true} if aspect method should
     * return value, otherwise {@code null}.
     */
    public abstract boolean after(Object target, Method method, Object[] args);
 
    /**
     * Invoked after exception.
     */
    public abstract boolean afterException(Object target, Method method, Object[] args, Throwable throwable);
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
 
        if (before(target, method, args)) {
            try {
                result = method.invoke(target, args);
            }
            catch (InvocationTargetException e) {
                afterException(args, method, args, e.getTargetException());
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        if (after(target, method, args)) {
            return result;
        }
        return null;
    }
 
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class="brush: java"&gt;
 &lt;pre&gt;&lt;code&gt;public class AopProxy {
 
    /**
     * Creates a proxy of given target and the aspect.
     */
    @SuppressWarnings("unchecked")
    public static &amp;lt;T&amp;gt; T proxyOf(T target, Class&amp;lt;? extends Aspect&amp;gt; aspectClass) {
        final Aspect aspect;
 
        try {
            aspect = ClassUtil.newInstance(aspectClass, target);
        }
        catch (Exception e) {
            throw new IllegalArgumentException("Can't create new instance of aspect class", e);
        }
 
        return (T) newProxyInstance(target.getClass().getClassLoader(), aspect, target.getClass().getInterfaces());
    }
 
    /**
     * Creates a proxy from given {@link Aspect}.
     */
    @SuppressWarnings("unchecked")
    public static &amp;lt;T&amp;gt; T proxyOf(Aspect aspect) {
        final Object target = aspect.getTarget();
        return (T) newProxyInstance(target.getClass().getClassLoader(), aspect, target.getClass().getInterfaces());
    }
 
    /**
     * Simple wrapper for javas {@code newProxyInstance}.
     */
    @SuppressWarnings("unchecked")
    public static &amp;lt;T&amp;gt; T newProxyInstance(ClassLoader classloader, InvocationHandler invocationHandler, Class&amp;lt;?&amp;gt;... interfaces) {
        if (interfaces.length == 0) {
            throw new IllegalArgumentException("No interfaces of target class found.");
        }
        return (T) Proxy.newProxyInstance(classloader, interfaces, invocationHandler);
    }
 
}
 &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="spring事务"&gt;Spring事务&lt;/h3&gt;

&lt;h3 id="springmvc"&gt;SpringMVC&lt;/h3&gt;</content></entry></feed>